import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.*;

public class Nurse extends User {
	private static Logger nurseLog = Logger.getLogger(Nurse.class.getName());
	
	static{
		ConsoleHandler console = new ConsoleHandler();
		console.setLevel(Level.FINEST);
		console.setFormatter(new ThunderDragonHtmlFormatter());
		nurseLog.addHandler(console);
		nurseLog.setLevel(Level.ALL);
		
		try {
			FileHandler fh = new FileHandler("NurseLog.txt");
			fh.setFormatter(new ThunderDragonHtmlFormatter());
			nurseLog.addHandler(fh);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			nurseLog.severe("Failure: Security exception thrown " +
					"when trying to create a new handler" +
					" in Nurse.java.");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			nurseLog.severe("Failure: I/O exception thrown" +
					" when trying to create a new handler " +
					"in Nurse.java.");
			e.printStackTrace();
		}
		
	}

	/**
	 * Crease a new Nurse in the medical system
	 * @param name Name of the Nurse
	 * @param username Username for system log in
	 * @param password Password for system log in
	 * @param address The address of the nurse
	 * @param phoneNum The phone number of the address
	 * @param email The email address of the Nurse
	 * @param age The age of the Nurse
	 */
	public Nurse(String name, String username, String password, String address,
			String phoneNum, String email, String age) {
		super(name, username, password, address, phoneNum, email, age);
		usertype = "nurse";
		nurseLog.entering(this.getClass().getName(), 
				"Nurse Constructor", new Object[] {name, username,
			password, address, phoneNum, email, age});
		nurseLog.exiting(this.getClass().getName(), "Nurse Constructor");
		}
		// TODO Auto-generated constructor stub
	
	public Nurse(){
		nurseLog.entering(this.getClass().getName(), "Nurse Default Constructor");
		nurseLog.exiting(this.getClass().getName(), "Nurse Default Constructor");
		usertype = "nurse";
	}
	
	public Nurse(User user){
		name = user.name;
		username = user.username;
		password = user.password;
		address = user.address;
		phoneNum = user.phoneNum;
		email = user.email;
		age = user.age;
		usertype = "nurse";
	}
	
	/**
	 * Creates a new Patient in the system.
	 * @param a Patient object as supplied by GUI
	 */
	public void createPatient(Patient a){
		UserDatabase.addUser(a, UserDatabase.PATIENT);
		nurseLog.finest("Patient created in class Nurse.");
	}
	
	/**
	 * Deletes a patient from the system.
	 * 
	 * It is suggested that the Nurse search for the patient 
	 * before removing as to have the correct username.
	 * @param patientUsername Username of the Patient to be removed.
	 */
	public void deletePatient(String patientUsername){
		UserDatabase.removeUser(patientUsername, UserDatabase.PATIENT);
		nurseLog.finest("Patient deleted in class Nurse.");
	}
	
	/**
	 * Edits a patient already in the system.
	 * @param patientUsername Username of the patient to be edited.
	 * @param a Patient object as supplied by GUI
	 */
	public void editPatient(String patientUsername, Patient a){
		ArrayList<Patient> results = 
			UserDatabase.searchPatient( patientUsername, 
					UserDatabase.USERNAME_SEARCH);
		results.get(0).editPatient(a);
		nurseLog.finest("Patient info edited in class Nurse.");
	}
	
	/**
	 * Views a patients information.
	 * @param patientUsername Username of the patient to be viewed.
	 * @return the Patient found from the method.
	 */
	public Patient viewPatient(String patientUsername){
		ArrayList<Patient> results = 
			UserDatabase.searchPatient( patientUsername, 
					UserDatabase.USERNAME_SEARCH);
		nurseLog.finest("Viewing patient in class Nurse.");
		return results.get(0);
	}
	
	/**
	 * Searches for a patient in the system by name.
	 * @param name Name of the patient being searched for
	 * @return Patient object as to be interpreted by GUI
	 */
	public ArrayList<Patient> searchPatient(String name){
		ArrayList<Patient> results = 
			UserDatabase.searchPatient( name, UserDatabase.NAME_SEARCH);
		nurseLog.finest("Searching for patient by name in class Nurse.");
		return results;
	}
	
	/**
	 * Creates a new treatment record to be added to the patients medical history.
	 * All parameters are to be provided by GUI.
	 * @param patientUsername Username of the patient being seen
	 * @param doctorUsername Username of the doctor seeing the patient
	 * @param nurseUsername Username of the attending nurse
	 * @param date Date of the appointment
	 * @param time Time of the appointment
	 * @param symptoms Symptoms the patient shows
	 * @param bloodPressure Patients blood pressure
	 * @param pulse Patients pulse
	 * @param temperature Patients temperature
	 * @param height Height of the patient
	 * @param weight Weight of the patient
	 * @param diagnosis Doctors diagnosis of the patient
	 */
	public void createRecord(String patientUsername, 
			String doctorUsername, String nurseUsername, 
			String date, String time, String symptoms, double bloodPressure,
			double pulse, double temperature, double height, double weight,  
			String diagnosis){
		Doctor d = 
			UserDatabase.searchDoctor( doctorUsername, 
					UserDatabase.USERNAME_SEARCH).get(0);
		Nurse nurse = 
			UserDatabase.searchNurse( nurseUsername, 
					UserDatabase.USERNAME_SEARCH).get(0);
		Patient patient = 
			UserDatabase.searchPatient( patientUsername, 
					UserDatabase.USERNAME_SEARCH).get(0);
		patient.medHistory.addTreatmentRecord
		(new TreatmentRecord(patient, date, time, d, 
				symptoms, nurse, bloodPressure, pulse,
				temperature, height, weight, diagnosis));
		nurseLog.finest("Record created in class Nurse.");
	}
	
	/**
	 * Views the appointment list of a patient.
	 * @param patientUsername Username of the patient
	 * @return ArrayList of the appointments to be interpreted by another class
	 */
	public ArrayList<Appointment> viewAppointments(String patientUsername){
		Patient patient = 
			UserDatabase.searchPatient( patientUsername, 
					UserDatabase.USERNAME_SEARCH).get(0);
		nurseLog.finest("veiwAppointments() called in class Nurse.");
		return patient.viewAppointments();
	}

	@Override
	public String toString() {
		return "Nurse";
	}
	
}